package com.yic.module.base.service.centeruser;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.centeruser.vo.*;
import com.yic.module.base.dal.dataobject.centeruser.CenterUserDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.centeruser.CenterUserConvert;
import com.yic.module.base.dal.mysql.centeruser.CenterUserMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;

/**
 * 工作中心人员绑定 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class CenterUserServiceImpl implements CenterUserService {

    @Resource
    private CenterUserMapper centerUserMapper;

    @Override
    public Long createCenterUser(CenterUserCreateReqVO createReqVO) {
        validateCenterUserExists(createReqVO.getCenterId(), createReqVO.getUserId());
        // 插入
        CenterUserDO centerUser = CenterUserConvert.INSTANCE.convert(createReqVO);
        centerUserMapper.insert(centerUser);
        // 返回
        return centerUser.getId();
    }

    @Override
    public void updateCenterUser(CenterUserUpdateReqVO updateReqVO) {
        // 校验存在
        validateCenterUserExists(updateReqVO.getId());
        // 更新
        CenterUserDO updateObj = CenterUserConvert.INSTANCE.convert(updateReqVO);
        centerUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteCenterUser(Long id) {
        // 校验存在
        validateCenterUserExists(id);
        // 删除
        centerUserMapper.deleteById(id);
    }

    @Override
    public void deleteCenterUserByCenterId(Long centerId) {
        centerUserMapper.deleteByCenterId(centerId);
    }

    private void validateCenterUserExists(Long id) {
        if (centerUserMapper.selectById(id) == null) {
            throw exception(CENTER_USER_NOT_EXISTS);
        }
    }

    private void validateCenterUserExists(Long centerId, Long userId) {
        CenterUserDO centerUserDO = centerUserMapper.selectByCenterAndUser(centerId, userId);
        if (ObjectUtil.isNotEmpty(centerUserDO)) {
            throw exception(CENTER_USER_EXISTS);
        }
    }

    @Override
    public CenterUserDO getCenterUser(Long id) {
        return centerUserMapper.selectById(id);
    }

    @Override
    public List<CenterUserDO> getCenterUserList(Collection<Long> ids) {
        return centerUserMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CenterUserDO> getCenterUserPage(CenterUserPageReqVO pageReqVO) {
        return centerUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CenterUserDO> getCenterUserList(CenterUserExportReqVO exportReqVO) {
        return centerUserMapper.selectList(exportReqVO);
    }

    @Override
    public Boolean checkCenterUserExists(Long centerId, Collection<Long> userIds) {
        Boolean resp = Boolean.TRUE;
        List<CenterUserDO> centerUserDOS = centerUserMapper.selectListByCenterId(centerId);
        Set<Long> centerUserSet = CollectionUtils.convertSet(centerUserDOS, CenterUserDO::getUserId);
        if (!centerUserSet.containsAll((Set) userIds)) {
            return Boolean.FALSE;
        }
        return resp;
    }

}
